package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.stu.server.entity.Department;
import com.xz.stu.server.service.DepartmentService;
import com.xz.stu.server.mapper.DepartmentMapper;
import com.xz.stu.server.vo.department.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 熊磊
* @description 针对表【school_department】的数据库操作Service实现
* @createDate 2025-03-01 15:18:50
*/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
    implements DepartmentService{

    @Override
    public Long createDepartment(DepartmentCreateReqVO createReqVO) {
        Department department = BeanUtils.toBean(createReqVO, Department.class);
        this.save(department);
        return department.getId();
    }

    @Override
    public void updateDepartment(DepartmentUpdateReqVO updateReqVO) {
        Department department = BeanUtils.toBean(updateReqVO, Department.class);
        this.updateById(department);
    }

    @Override
    public void deleteDepartment(List<Long> id) {
        this.removeByIds(id);
    }

    @Override
    public DepartmentRespVO getDepartment(Long id) {
        Department department = this.getById(id);
        if (department != null) {
            return BeanUtils.toBean(department, DepartmentRespVO.class);
        }
        return null;
    }

    @Override
    public PageResult<DepartmentRespVO> getDepartmentPage(DepartmentPageReqVO pageVO) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasText(pageVO.getDeptName()) , Department::getDeptName, pageVO.getDeptName())
                .like(StringUtils.hasText(pageVO.getDean()), Department::getDean, pageVO.getDean());
        Page<Department> page = this.page(new Page<>(pageVO.getPageNo(), pageVO.getPageSize()), lambdaQueryWrapper);
        if (page != null) {
            return new PageResult<>(BeanUtils.toBean(page.getRecords(), DepartmentRespVO.class), page.getTotal());
        }
        return null;
    }

    @Override
    public List<DepartmentSimpleRespVO> getSimpleDepartments() {
        List<Department> departmentList = this.list();
        return BeanUtils.toBean(departmentList, DepartmentSimpleRespVO.class);
    }
}




